package com.px.service;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class MaiServiceImpl implements MaiService {

    //spring框架自带的邮件服务类
    @Resource
    private JavaMailSender javaMailSender;
    /**
     * qq邮箱
     *
     * @param from    发件人
     * @param toEmail 收件人
     * @param code    邮箱
     * @return
     * @throws Exception
     */
    @Override
    public boolean mailMessage(String from, String toEmail, String code) throws Exception {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            //发件人
            simpleMailMessage.setFrom(from);
            //邮件主题
            simpleMailMessage.setSubject("接收验证码");
            //收件人，也就是获取的邮箱
            simpleMailMessage.setTo(toEmail);
            //发送的日期
            simpleMailMessage.setSentDate(new Date());
            //正文
            simpleMailMessage.setText("验证码是："+code);

            javaMailSender.send(simpleMailMessage);
            return true;
    }
}
